$(function () {

    getReviewers({});

    // 初始化页面上的 datetimepicker
    insDatetimePicker('.is-date');

    layui.use(['form', 'layer'], function () {
        var form = layui.form(),
            layer = layui.layer,
            total_amount = 0,
            account_payable = 0;

        // 监听借款信息中是否借款选项是否开启
        form.on('select(is-loan)', function (data) {
            if (data.value == '是') {
                $('.loan-table .loan-bill').attr('disabled', false);
                $('.loan-table .loan-amount').attr('disabled', false);
            } else {
                // 如果借款为否则清空值并禁用组件
                $('.loan-table .loan-bill').val('');
                $('.loan-table .loan-amount').val('');
                $('.loan-table .loan-bill').attr('disabled', true);
                $('.loan-table .loan-amount').attr('disabled', true);
                // 当是否借款选项选择否时，重新计算金额
                listenLoanAmountChange(0);
            }
        });

        // 添加明细项
        $('#add-detail-item').click(function () {
            // 获取明细数量
            var item_qutatity = $(this).prev().find('tbody tr').length;
            // 去除最后一列合计的行
            item_qutatity = item_qutatity - 2;
            var html = [
                '<tr>',
                    '<td class="valign-middle">' + (item_qutatity + 1) + '</td>',
                    '<td class="valign-middle">',
                        '<input type="text" name="details[' + (item_qutatity + 1) + '][date]" lay-verify="required" readonly class="layui-input is-date">',
                    '</td>',
                    '<td class="valign-middle">',
                        '<input type="text" name="details[' + (item_qutatity + 1) + '][location]" lay-verify="required" class="layui-input">',
                    '</td>',
                    '<td class="valign-middle">',
                        '<input type="text" name="details[' + (item_qutatity + 1) + '][days]" lay-verify="required|number" class="layui-input">',
                    '</td>',
                    '<td class="valign-middle">',
                        '<input type="text" name="details[' + (item_qutatity + 1) + '][flight_ticket_fee]" lay-verify="money" class="layui-input is-money">',
                    '</td>',
                    '<td class="valign-middle">',
                        '<input type="text" name="details[' + (item_qutatity + 1) + '][travel_fee]" lay-verify="money" class="layui-input is-money">',
                    '</td>',
                    '<td class="valign-middle">',
                        '<input type="text" name="details[' + (item_qutatity + 1) + '][km]" lay-verify="km" oninput="onInput(event)" class="layui-input">',
                    '</td>',
                    '<td class="valign-middle">',
                        '<input type="text" name="details[' + (item_qutatity + 1) + '][fuels]" lay-verify="money" class="layui-input is-money" readonly>',
                    '</td>',
                    '<td class="valign-middle">',
                        '<input type="text" name="details[' + (item_qutatity + 1) + '][tolls]" lay-verify="money" class="layui-input is-money">',
                    '</td>',
                    '<td class="valign-middle">',
                        '<input type="text" name="details[' + (item_qutatity + 1) + '][transportation_fee]" lay-verify="money" class="layui-input is-money">',
                    '</td>',
                    '<td>',
                        '<input type="text" name="details[' + (item_qutatity + 1) + '][stay_fee]" lay-verify="money" class="layui-input is-money">',
                    '</td>',
                    '<td>',
                        '<input type="text" name="details[' + (item_qutatity + 1) + '][allowance]" lay-verify="money" class="layui-input is-money">',
                    '</td>',
                    '<td>',
                        '<input type="text" name="details[' + (item_qutatity + 1) + '][other]" lay-verify="money" class="layui-input is-money">',
                    '</td>',
                    '<td class="valign-middle">',
                        '<input type="text" name="details[' + (item_qutatity + 1) + '][subtotal]" class="layui-input subtotal" readonly>',
                    '</td>',
                    '<td>',
                        '<button type="button" class="btn btn-danger btn-sm remove-detail-item"><i class="icon-remove"></i></button>',
                    '</td>',
                '</tr>'
            ].join('');
            $(this).prev().find('tbody tr:nth-last-child(2)').before(html);
            insDatetimePicker('.is-date');
            // 添加明细后更新form组件避免出现无响应或渲染失败为空等现象
            form.render();
        });

        // 删除明细项
        $('.clfbx-items-table').on('click', '.remove-detail-item', function () {
            var tr_len = $('.clfbx-items-table tbody').children().length;
            // 当移除明细时检查是否小于 3 项，其中 2 项为合计栏位必须保留
            if (tr_len <= 3) {
                layer.msg('至少保留一项明细', {
                    icon: 0,
                    time: 2000
                });
            } else {
                $(this).parent().parent().remove();
                // 当明细项被删除后重新计算小计金额
                listenSubtotalChange();
                // 修正明细项删除后的序列错误
                $('.clfbx-items-table tbody tr').each(function (index) {
                    // 排除合计栏位的序列
                    if (index + 1 < tr_len - 2) {
                        $(this).find('td:first-child').text(index + 1);
                    }
                });
            }
        });

        // 监听金额变化
        $('.clfbx-items-table').bind('input propertychange', '.is-money', function (e) {
            listenTotalAmountChange();
        });

        function listenTotalAmountChange()
        {
            var tr_len = $('.clfbx-items-table tbody').children().length;
            $('.clfbx-items-table tbody tr').each(function (index) {
                // 排除掉合计栏位
                if (index <= tr_len - 2) {
                    var subtotal = 0;
                    $(this).find('.is-money').each(function (idx) {
                        var cur_val = $(this).val();
                        subtotal += parseFloat($(this).val() ? $(this).val() : 0);
                    });
                    // 保留两位小数
                    subtotal = parseFloat(subtotal);
                    $(this).find('.subtotal').val(moneyFormat(subtotal));
                }
            });
        }

        $('.clfbx-items-table').on('input propertychange', '.km', function (e) {
            console.log(e.target.value);
        });

        // 监听小计变化
        $('.clfbx-items-table').bind('input propertychange', '.subtotal', function (e) {
            listenSubtotalChange();
        });

        // 监听借款金额变化
        $('.loan-amount').bind('input propertychange', function (e) {
            // 获取到当前值
            var current_val = $(e.target).val();
            listenLoanAmountChange(current_val);
        });

        // 监听借款金额变化
        function listenLoanAmountChange(loan_amount) {
            if (total_amount) {
                var ap = parseFloat(total_amount - loan_amount);
                // 对应栏位赋值(大写)
                $('.clfbx-items-table tbody tr .uppercase-account-payable').val(Arabia_to_Chinese(ap));
                // 对应栏位赋值(小写)
                $('.clfbx-items-table tbody tr .account-payable').val(moneyFormat(ap));
            }
        }

        // 监听小计变化
        function listenSubtotalChange() {

            var details_len = $('.clfbx-items-table tbody').children().length,

            // 明细数量
            loan_amount = $('.loan-table .loan-amount').val() ? $('.loan-table .loan-amount').val() : 0; // 借款金额

            // 如果明细只存在一条则直接获取计算，如果多条则遍历
            if (details_len > 3) {
                var tmp_total_amount = 0;
                $('.clfbx-items-table tbody .subtotal').each(function (index) {
                    if ($(this).val()) {
                        // 计算合计金额
                        tmp_total_amount += moneyToNumber($(this).val());
                    }
                });
                total_amount = tmp_total_amount;
            } else {
                // 获取第一条明细条目的小计
                var subtotal_of_first_detail = $('.clfbx-items-table tbody tr:first-child .subtotal').val();
                if (subtotal_of_first_detail) {
                    // 去除掉人民币符号并将类型转换为浮点型
                    subtotal_of_first_detail = moneyToNumber(subtotal_of_first_detail);
                }
                total_amount = subtotal_of_first_detail;
            }

            if (total_amount) {
                // 如果存在借款金额则减去借款金额
                account_payable = parseFloat(total_amount - loan_amount);
                // 将合计金额和应付金额转换为大写
                var uppercase_total_amount = Arabia_to_Chinese(total_amount),
                    uppercase_account_payable = Arabia_to_Chinese(account_payable);
                // 对应栏位赋值(大写)
                $('.clfbx-items-table tbody tr .uppercase-total-amount').val(uppercase_total_amount);
                $('.clfbx-items-table tbody tr .uppercase-account-payable').val(uppercase_account_payable);
                // 对应栏位赋值(小写)
                $('.clfbx-items-table tbody tr .total-amount').val(moneyFormat(total_amount));
                $('.clfbx-items-table tbody tr .account-payable').val(moneyFormat(account_payable));
            }
        }

    });
});